package com.wolfeyes.system.service.impl;

import com.wolfeyes.system.mapper.SysDictMapper;
import com.wolfeyes.common.utils.translation.SysDictDO;
import com.wolfeyes.system.service.SysDictService;
import com.wolfeyes.common.utils.character.ParamUtils;
import com.wolfeyes.common.utils.character.StringUtils;
import com.wolfeyes.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @see Talk is cheap, Show me the code. -- Linus Torvalds
 * @title 字典表信息服务层-实现类
 * @author yanyljava
 * @email yanyljava@qq.com
 * @create 2022-01-15 10:21
 */
@Service
public class SysDictServiceImpl implements SysDictService {
    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public SysDictDO get(Long id) {
        return sysDictMapper.get(id);
    }

    @Override
    public List<SysDictDO> list(Map<String, Object> param) {
    	String sort = (String)param.get("sort");
    	if(ParamUtils.isNull(sort)){
    		param.put("orderBySortAsc", "order_by_sort_asc");
    	}
        return sysDictMapper.list(param);
    }

    @Override
    public int count(Map<String, Object> map) {
        return sysDictMapper.count(map);
    }

    @Override
    public int save(SysDictDO dict) {
        return sysDictMapper.save(dict);
    }

    @Override
    public int update(SysDictDO dict) {
        return sysDictMapper.update(dict);
    }

    @Override
    public int remove(Long id) {
        return sysDictMapper.remove(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return sysDictMapper.batchRemove(ids);
    }

    @Override

    public List<SysDictDO> listType() {
        return sysDictMapper.listType();
    }

    @Override
    public String getName(String type, String value) {
        Map<String, Object> param = new HashMap<String, Object>(16);
        param.put("type", type);
        param.put("value", value);
        String rString = sysDictMapper.list(param).get(0).getName();
        return rString;
    }

    @Override
    public List<SysDictDO> getHobbyList(SysUser sysUser) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "hobby");
        List<SysDictDO> hobbyList = sysDictMapper.list(param);

        if (StringUtils.isNotEmpty(sysUser.getHobby())) {
            String userHobbys[] = sysUser.getHobby().split(";");
            for (String userHobby : userHobbys) {
                for (SysDictDO hobby : hobbyList) {
                    if (!Objects.equals(userHobby, hobby.getId().toString())) {
                        continue;
                    }
                    hobby.setRemarks("true");
                    break;
                }
            }
        }

        return hobbyList;
    }

    @Override
    public List<SysDictDO> getSexList() {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "sex");
        param.put("orderBySortAsc", "order_by_sort_asc");
        return sysDictMapper.list(param);
    }

    @Override
    public List<SysDictDO> listByType(String type) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", type);
        param.put("orderBySortAsc", "order_by_sort_asc");
        return sysDictMapper.list(param);
    }

}
